from flask import Blueprint
from flask import request, session
import json,re
from src.router.login import Login
bp = Blueprint("register", __name__)

from src.database.getdb import database 
db=database().create_db()


@bp.route('/register')
def entrance():
    data={'code':1}
    if request.method == "GET":
        username = request.args.get("username").strip()
        password = request.args.get("password").strip()
        email = request.args.get("email").strip()

        if username and password and email:
            sql="insert into dzpk_user (username,password,email)values('%s','%s','%s')"%(username,password,email)
            print(sql)
            with  db.cursor() as cursor:
                try:
                    cursor.execute(sql)
                    db.commit()
                    result = Login().entrance(username,password)
                    return result
                except Exception as err: 
                    print(err)

                    result =re.search("Duplicate entry.*key 'email'", str(err))
                    result1 =re.search("Duplicate entry.*key 'username'", str(err))
                    if result:
                        data={'code':0,'mess':'邮箱已存在'}                 
                    elif result1:
                        data={'code':0,'mess':'用户名已存在'}
                    else:
                        data={'code':0,'mess':'用户名已存在'}

                cursor.close()
        else:
            data={'code':0,'mess':'not null'}

    
	  
    return json.dumps(data)











